<?php
namespace Com\Pay;

class YinxuntongPayAlipay{

    private $config = array();
    private $allparam =array(); //所有提交的参数
    private $sign;

    public function __construct($send_data){
        $this->config = $send_data;
    }

    /*生成支付连接*/
    public function getUrl($order_info,$other = array()){
        if( empty($order_info['paymoney']) or $order_info['paymoney']<=0 ){
            return false;
        }

        $url='http://mp.silverpay.cn/Payapi/index/pay';         // 支付请求地址
        
        $notify_url = 'http://'. $_SERVER['HTTP_HOST'] . '/Api/NotifyCallback/YingxuntongNotify?bank_type=alipay_pay';
        $ret_url    = $order_info['return_url'];

        $fee        = $order_info['paymoney'];
        $fee        = $fee * 100;

        $postdata = array(
               'mch_id'        => $this->config['mch_id'],      // 商户号
               'order_time'    => time(),                       // 创建时间
               'order_number'  => $order_info['ordernumber'],  // 订单号
                'goodsname'     => 'What',
                 'pay_money'     => $fee,                         // 金额 分
               'ret_url'       => $ret_url,                     // 前台通知地址
               'paycode'    => 'YSPAY',                  // 异步回调地址
               'notify_url'    => $notify_url,                  // 异步回调地址
               // 11 微信公众号支付 12 微信扫描支付 21 支付宝支付 22 支付宝扫描 31 QQ钱包支付 32 QQ钱包扫描支付
               'paymenttypeid' => '22',                         // 支付类型
            );
        $this->setParam($postdata);
        $postdata = $this->allparam;
        $get_string = '';
        foreach ($postdata as $key => $value) {
            if($get_string==''){
                $get_string .="?{$key}={$value}";
            } else {
                $get_string .="&{$key}={$value}";
            }
        }

        $get_url = $url.$get_string;
        $resjson = https_request($get_url);
        $res  = json_decode($resjson,true);
        return $res['qrcode'];
    }

    //回调方法，把返回数据处理成统一规定返回
    public function response(){
        $post = parseData();
        if(empty($post)){
            return false;
        }

        if($post['ret_code']==200 && $post['order_number']){

            //验证签名
            $this->setParam($post);
            if($this->sign != $post['sign']){
                return false;
            } else {
                return true;
            }
        } else {
            return false;
        }
        return false;
    }

    private function gbkToUtf8($arr){
        if(is_array($arr)){
            $res = array();
            foreach($arr as $k => $v){
                $res[$k] = $this->gbkToUtf8($v);
            }
            return $res;
        }else{
            return mb_convert_encoding ( $arr, "UTF-8", "GBK" );
        }
    }

    /**
     * 对数组排序
     * @param $para 排序前的数组
     * return 排序后的数组
     */
    private function argSort($para)
    {
        ksort($para);
        reset($para);
        return $para;
    }

    private function setParam($param){
        $str = "";
        unset($param['bank_type']);
        $param = $this->argSort($param);
        foreach($param as $key=>$val){
            if($key!='sign'){
                $str.=$key."=".$val."&";
            }
        }

        $str           .= 'key='.$this->config['mch_key'];
        $sign           = md5($str);
        $sign           = strtoupper($sign);
        $param['sign']  = $sign;
        $this->sign     = $sign;
        $this->allparam = $param;

    }
}
